package com.jwdf.uda.utils;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.List;

/**
 * 类说明：工程工具类
 * 作者：huangqiuxin on 2016/3/29 09:05
 * 邮箱：648859026@qq.com
 */
public class MobUtil {

    /**
     * 检查栈顶是不是某一个activity
     *
     * @param context
     * @param activityName
     * @return
     */
    public static boolean isActivityOnTop(Context context, String activityName) {
        try {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> list = activityManager.getRunningTasks(50);
            for (ActivityManager.RunningTaskInfo runningTaskInfo : list) {
                if (runningTaskInfo.baseActivity.getPackageName().equals(context.getPackageName())) {
                    ComponentName componentName = runningTaskInfo.topActivity;// 获取最顶层的activity
                    if (componentName.getClassName().indexOf(activityName) != -1) {
                        return true;
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获得自身版本号
     *
     * @param context
     * @return
     */
    public static int getSelfVersionCode(Context context) {
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            if (info != null) {
                return info.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return -1;
    }

    /**
     * 获得自身版本名
     *
     * @param context
     * @return
     */
    public static String getSelfVersionName(Context context) {
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            if (info != null) {
                return info.versionName;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "1.0";
    }

}
